package piirto;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;

public class GraphingData extends JPanel {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	
	int[] data = {
        0, 3, 5, 8, 14, 19, 74, 87, 54, 77, 34, 43, 46, 34, 36, 0, 44, 46, 45, 33, 0, 0, 33, 11, 14, 33, 22, 11, 0,  0,  
        109, 55, 48, 60, 49, 36, 38, 27, 20, 18, 45, 53, 65, 23, 45, 32, 44, 54, 45, 31, 0, 0, 33, 11, 55, 32, 22, 28, 20, 18, 15, 0,
        0, 3, 5, 8, 14, 19, 74, 87, 54, 77, 34, 43, 46, 34, 36, 0, 44, 46, 45, 33, 0, 0, 33, 11, 14, 33, 22, 11, 0,  0,  
        109, 55, 48, 60, 49, 36, 38, 27, 20, 18, 45, 53, 65, 23, 45, 32, 44, 54, 45, 31, 0, 0, 33, 11, 55, 32, 22, 28, 20, 18, 15, 0,
 
    };
  
    final int PAD =data.length;
    double d=PAD/15;
    final double TICK_DIST =0.5;
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        
        double ppmLength=6;
        double sca=50;
        double tick=0.5;
        double steps=ppmLength/(tick/10);
        float width = getWidth();
        System.out.println(width);
        int h = getHeight();
        System.out.print(getWidth());
        // Draw ordinate.
        //g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
        // Draw abcissa.
        g2.draw(new Line2D.Double(0, h-PAD, width, h-PAD));
        // Draw labels.
       
        
        Font font = g2.getFont();
        FontRenderContext frc = g2.getFontRenderContext();
        LineMetrics lm = font.getLineMetrics("0", frc);
        float sh = lm.getAscent() + lm.getDescent();
        // Ordinate label.
        String s = "";
        float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent();
        for(int i = 0; i < s.length(); i++) {
            String letter = String.valueOf(s.charAt(i));
            float sw = (float)font.getStringBounds(letter, frc).getWidth();
            float sx = (PAD - sw)/2;
            g2.drawString(letter, sx, sy);
            sy += sh;
        }
        
        s = "Frequence";
        sy = h - PAD + (PAD - sh)/2 + lm.getAscent();
        float sw = (float)font.getStringBounds(s, frc).getWidth();
        float sx = (width - sw)/2;
        g2.drawString(s, sx, sy);
        // Draw lines.
        //double xInc = (double)(w - 2*PAD)/(data.length-1);
        
        double  xInc = (double)(width /(data.length-1));
        double scale = (double)(h - 2*PAD)/getMax();
   
        double tickscale=width/(ppmLength*sca); 
        
        g2.setPaint(Color.green.darker());
        for (double k = TickMarks.NumOfTicMarks(ppmLength); k >=0; k-=1)
            g2.drawLine((int)(k*TickMarks.increment(width,ppmLength)),h-PAD,(int)(k*tickscale),h-PAD+5);
        for (double k = steps; k >=0; k-=10)
            g2.drawLine((int)(k*tickscale),h-PAD,(int)(k*tickscale),h-PAD+10);
        for(double i = 0; i < data.length-1; i++) {
            System.out.print(i*xInc+" ");
        	double x1 = 1920 - i*xInc;
            double y1 = h - PAD - scale*data[(int) i]-20;
            double x2 = 1920 - (i+1)*xInc;
            double y2 = h - PAD - scale*data[(int) (i+1)]-20;
            g2.draw(new Line2D.Double(x1, y1, x2, y2));
            
        }
       
        // Mark data points.
        //g2.setPaint(Color.red);
        //for(int i = 0; i < data.length; i++) {
          //  double x = PAD + i*xInc;
           // double y = h - PAD - scale*data[i];
            //g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
        }
    

   
		
	


	private int getMax() {
        int max = -Integer.MAX_VALUE;
        for(int i = 0; i < data.length; i++) {
            if(data[i] > max)
                max = data[i];
        }
        return max;
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new GraphingData());
       // f.setSize(400,400);
       // f.setLocation(200,200);
        f.setExtendedState(f.getExtendedState()|JFrame.MAXIMIZED_BOTH);
        f.setVisible(true);
    }
}